package com.hqj.demo01;

/**
 * 抽象类与抽象方法的练习
 * @author HQJ
 * 1、有问题类Question
 * 	属性：text，options[]
 * 	text:选择题题目
 * 	options[]：选择题选项。如：A:1,B:2…………
 * 	提供相应的构造方法初始化属性
 * 	提供属性的get\set方法
 * 	有抽象的检查输入答案是否正确的方法check()，参数为String answer 你回答的答案
 *  有输出题目和选项的方法print()
 *
 * 	2、有单项选择题类SingleChoice 继承 Question类
 * 	有属性char answer
 * 	answer:当你新增选择题是，该选择题的正确答案
 * 	提供相应的构造方法初始化属性
 * 	重写父类方法check（），判断回答是否正确
 *
 * 	3、有多项选择题类MultiChoice继承 Question类
 * 	有属性String answer
 * 	char cs[] = answer.tocharArray();
 * 	answer:当你新增选择题是，该选择题的正确答案
 * 	提供相应的构造方法初始化属性
 * 	重写父类方法check（），判断回答是否正确
 *
 * 	4、测试类
 * 	创建几道选择题，存放到List<Qustion> list 中
 * 	循环题目就行回答，输出回答结果(正确或错误)
 */
public class Exercise03 {

    public static void main(String[] args) {
        Question q = new SingleCheck("1+1=?",new String[]{"A:1","B:2","C:3","D:4"},'b');
        q.print();
        boolean b = q.check("B");
        System.out.println(b?"正确":"错误");

        Question q2 = new MultChechk("广西的城市?",new String[]{"南宁","北京","上海","桂林"},"AD");
        q2.print();
        b = q2.check("B");
        System.out.println(b?"正确":"错误");
    }
}

abstract class Question{
    private String text;
    private String []options;

    public Question() {}

    public Question(String text, String[] options) {
        this.text = text;
        this.options = options;
    }

    public abstract boolean check(String answer);

    /**
     * 输出题目
     */
    public void print(){
        System.out.println("题目:"+this.text);
        for(String option:this.options){
            System.out.print(option+"\t");
        }
        System.out.println("\n");
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String[] getOptions() {
        return options;
    }

    public void setOptions(String[] options) {
        this.options = options;
    }
}

class SingleCheck extends  Question{

    private char answer;

    public SingleCheck() {}

    public SingleCheck(String text, String[] options, char answer) {
        super(text, options);
        this.answer = answer;
    }

    @Override
    public boolean check(String answer) {//B  BA AB    "1+"
        return (this.answer+"").equalsIgnoreCase(answer);
    }
}

class MultChechk extends Question{

    private String answer; //AB  A  B

    public MultChechk() {}

    public MultChechk(String text, String[] options, String answer) {
        super(text, options);
        this.answer = answer;
    }

    @Override
    public boolean check(String answer) {//bA   回答每一个字符在正确答案中出现就行
        boolean flat = false;
        if(this.answer.length()!= answer.length()) return false;

        char rs[] = this.answer.toUpperCase().toCharArray();
        char ys[] = answer.toUpperCase().toCharArray();

        for(char y:ys){//   C  B
            flat = false;
            for (char r:rs){//A B
                if(y==r){
                    flat = true;
                    break;
                }
            }
            if(!flat){
                break;
            }
        }


        /* flat = true;
        //字符串-包含contains  ABC A
        for(char y:ys){
            if(!this.answer.contains(y+"")){
                flat = false;
                break;
            }
        }*/

        return flat;
    }
}